package com.pxmeta.app.controller.game;


import cn.dev33.satoken.annotation.SaCheckLogin;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.pxmeta.app.controller.game.request.UserOrderChargingDTO;
import com.pxmeta.app.controller.game.request.VoiceConnectionRecordSaveDTO;
import com.pxmeta.app.controller.game.request.VoiceConnectionScoringDTO;
import com.pxmeta.app.controller.game.request.VoiceConnectionVerifyDTO;
import com.pxmeta.common.core.domain.AjaxResult;
import com.pxmeta.common.utils.SecurityUtils;
import com.pxmeta.skill.entity.Skill;
import com.pxmeta.party.entity.SkillPrice;
import com.pxmeta.party.entity.vo.*;
import com.pxmeta.skill.entity.UserSkill;
import com.pxmeta.party.service.ISkillPriceService;
import com.pxmeta.skill.service.ISkillService;
import com.pxmeta.order.service.IUserOrderRecordService;
import com.pxmeta.skill.service.IUserSkillService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@Api(value = "声动连线",tags = "声动连线")
@RequiredArgsConstructor
@RequestMapping("/voiceConnection")
public class VoiceConnectionController {

    private final IUserSkillService userSkillService;
    private final IUserOrderRecordService userOrderRecordService;
    private final ISkillService skillService;
    private final ISkillPriceService servicePriceService;


    @SaCheckLogin
    @GetMapping("/getHotSkillList")
    @ApiOperation("获取热门技能列表")
    public AjaxResult<List<Skill>> getHotSkillList(){
       List<Skill> skills =  skillService.getHotSkillList().stream().limit(4).collect(Collectors.toList());
       return AjaxResult.success(skills);
    }


    @SaCheckLogin
    @GetMapping("/getAllSkillList")
    @ApiOperation("获取全部技能列表")
    public AjaxResult<List<SkillCategoryVO>> getAllSkillList(){
        List<SkillCategoryVO> skills =  skillService.getAllSkillList();
        return AjaxResult.success(skills);
    }



    @SaCheckLogin
    @PostMapping("/submitVerify")
    @ApiOperation("提交审核")
    public AjaxResult submit(@RequestBody VoiceConnectionVerifyDTO dto){
        Long userId = SecurityUtils.getUserId();
        String desc = dto.getDesc();
        Integer type = dto.getType();
//        userSkillService.submitVerify(userId, type,desc);
        return AjaxResult.error("请更新软件后再使用！");
    }

    @GetMapping("/getOvertimeFees")
    @ApiOperation("获取加时费用")
    public AjaxResult<List<ServiceOvertimeFeeVO>> getOvertimeFees(@RequestParam Integer type){
        List<ServiceOvertimeFeeVO> list = servicePriceService.getListBySkillId(type);
        return AjaxResult.success(list);
    }



    @GetMapping("/getUserList")
    @SaCheckLogin
    @ApiOperation(value = "获取用户列表")
    public AjaxResult<List<UserSkillVO>> getVoiceList(@RequestParam Integer page,
                                                      @RequestParam Integer size,
                                                      @RequestParam(required = false) Integer type){

        List<UserSkillVO> list = userSkillService.getList(null,type,page, size,null);
        return AjaxResult.success(list);
    }


    @PostMapping("/saveConnectionRecord")
    @ApiOperation("保存连线记录")
    @SaCheckLogin
    public AjaxResult saveRecord(@RequestBody VoiceConnectionRecordSaveDTO saveDTO){
        Long targetUserId = saveDTO.getTargetUserId();
        Long userId = SecurityUtils.getUserId();
        Long recordId = userOrderRecordService.saveRecord(userId, targetUserId, saveDTO.getType());

        HashMap<String, Object> map = Maps.newHashMap();
        map.put("recordId", recordId);

        return AjaxResult.success(map);
    }




    @SaCheckLogin
    @ApiOperation("获取服务列表")
    @GetMapping("/getServiceList")
    public AjaxResult getServiceList(@RequestParam(required = false) Long userId){
        List<Skill> list = skillService.list();

        if (null == userId){
            userId = SecurityUtils.getUserId();
        }

        List<UserSkill> userServiceList = userSkillService.getUserList(userId);
        List<SkillVO> finalList = Lists.newLinkedList();
        for (Skill skill : list) {
            SkillVO vo = new SkillVO();
            BeanUtils.copyProperties(skill, vo);
            for (UserSkill userService : userServiceList) {
                if (skill.getCategory() == 2 && userService.getSkillId().equals(skill.getId())){
                    vo.setStatus(userService.getStatus());
                    finalList.add(vo);
                }
            }
        }
        return AjaxResult.success(finalList);
    }


    @PostMapping("/scoring")
    @ApiOperation("连线打分")
    @SaCheckLogin
    public AjaxResult scoring(@RequestBody VoiceConnectionScoringDTO scoringDTO){
        userOrderRecordService.scoring(scoringDTO.getRecordId(), scoringDTO.getScore());
        return AjaxResult.success();
    }


    @GetMapping("/getServicePrice")
    @ApiOperation("根据服务类型获取用户服务价格")
    @SaCheckLogin
    public AjaxResult<ServicePriceDetailVO> getServicePrice(@RequestParam Long userId,
                                      @RequestParam Integer type){
        LambdaQueryWrapper<UserSkill> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(UserSkill::getUserId, userId);
        SkillPrice skillPrice = servicePriceService.getDefaultBySkillId(type);
        ServicePriceDetailVO vo = new ServicePriceDetailVO();
        vo.setPrice(skillPrice.getPrice());
        vo.setServicePriceId(skillPrice.getId());
        vo.setDuration(skillPrice.getDuration());
        return AjaxResult.success(vo);
    }

    @PostMapping("/charging")
    @ApiOperation("连线扣费")
    @SaCheckLogin
    public AjaxResult charging(@RequestBody UserOrderChargingDTO chargingDTO){
        Long userId = SecurityUtils.getUserId();
        userOrderRecordService.charging(userId,chargingDTO.getRecordId(), chargingDTO.getServicePriceId());
        return AjaxResult.success();
    }
}
